<template>
  <div class="add-form">
    <el-dialog :title="titleInfo.text+titleInfo.pageTitle" :visible.sync="dialogFormVisible" width="30%">
      <el-form
        :rules="rules"
        ref="Subject"
        :model="Subject"
        label-position="left"
        label-width="150px"
        style="width: 80%; margin-left:10px;"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="Subject.subjectName"></el-input>
          <!-- {{Subject.subjectName}} -->
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch v-model="Subject.isFrontDisplay "></el-switch>
          <!-- {{Subject.isFrontDisplay}} -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  @click="hClear">{{$t('table.cancel')}}</el-button>
        <el-button type="primary" @click="hConfirm">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { add, update } from '@/api/hmmm/subjects'
export default {
  name: 'SubjectsAdd',
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    Subject: {
      type: Object,
      required: true
    },
    getlist: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      // value: this.Subject.isFrontDisplay,
      // 编辑时使用
      // form: {
      //   // subjectName: '',
      //   isFrontDisplay: true
      // },
      dialogFormVisible: false,
      // 表单验证
      rules: {
        subjectName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  // computed: {},
  methods: {
    // 弹层显示
    dialogFormV () {
      this.dialogFormVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogFormVisible = false
    },
    hClear () {
      this.dialogFormH()
      this.Subject.subjectName = ''
      this.Subject.isFrontDisplay = false
    },
    hConfirm () {
      this.$refs.Subject.validate(async valid => {
        if (valid) {
          console.log('jiaoyan', valid)
          this.createData()
        }
      })
    },
    // 添加科目 修改科目
    async createData () {
    // 1.判断 有没有 Subject.id,如果有id则为编辑
      if (this.Subject.id) {
        try {
          // 修改学科
          const data = this.Subject
          const res = await update(data)
          console.log('修改成功', res)
          // 刷新页面
          this.getlist()
          // 提示用户
          this.$message.success('修改成功')
          // 关闭弹框
          this.dialogFormH()
          this.Subject.subjectName = ''
          this.Subject.isFrontDisplay = false
        } catch (err) {
          this.$message.error('修改失败！')
        }
      } else {
        try {
          // 添加学科
          const data = this.Subject
          // console.log('传参...', this.Subject.subjectName, this.Subject.isFrontDisplay)
          const res = await add(data)
          console.log('添加成功...', res)
          // 刷新页面
          this.getlist()
          // 提示用户
          this.$message.success('添加成功！')
          // 关闭
          this.dialogFormH()
          this.Subject.subjectName = ''
          this.Subject.isFrontDisplay = false
        } catch (err) {
          this.$message.error('添加失败！')
        }
      }
    }
    // this.$emit('add', this.Subject.subjectName)
  }
  // 挂载结束

  // mounted: function () {},
  // 创建完毕状态
  // created () {
  //   this.getCityData()
  // },
  // // 组件更新
  // updated: function () {}
}
</script>
<style>

.el-form--label-left .el-form-item__label {
  text-align: right;
}
.el-form-item--medium {
  margin-bottom: 22px;
}
.el-dialog__footer {
  text-align: center;
}
</style>
